//
//  CareTableView.m
//  1gene
//
//  Created by 1jiyin on 16/4/29.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "CareTableView.h"
@interface CareTableView()

@property(nonatomic,copy)NSMutableArray *myDataArray;


@property(nonatomic,copy)NSArray *idArray;

@property(nonatomic,copy)NSString *userId;

@end
@implementation CareTableView
static int tiebaAnimationState=0;
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style UserId:(NSString *)userId
{
    if (self=[super initWithFrame:frame style:style]) {
        _userId=userId;
        _myDataArray=[NSMutableArray array];
        self.showsVerticalScrollIndicator=NO;
        self.delegate=self;
        self.dataSource=self;
        self.backgroundColor=[UIColor whiteColor];
        self.tableFooterView=[UIView new];
        self.tableHeaderView=[UIView new];
        
        [self getHttpData];
        
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self initData];
        self.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getHttpData];
        }];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTableView:) name:@"ReloadCareTableView" object:nil];
    }
    return self;
}



//根据下一页的改动来修改tableView
-(void)changeTableView:(NSNotification *)notify
{
    //[[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadTieBaTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    NSDictionary *dic=notify.userInfo;
    NSLog(@"收到的通知==============%@",dic);
    NSInteger myTag=[self returnIndexPath:dic[@"topic_id"]];
    if (myTag<0) {
        return ;
    }else{
        
        
        
        UIImageView *imgView=(id)[self viewWithTag:[self returnIndexPath:dic[@"topic_id"]]];
        NSLog(@"=========%ld",imgView.tag);
        TieBaModel *model1=_myDataArray[imgView.tag];
        tiebaAnimationState=1;
        
        NSInteger zanState=[model1.is_like integerValue];
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        if (zanState==0) {
            //点赞
            imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_yes"].CGImage;
            
            NSLog(@"点赞");
            
            NSDictionary *dic=_tieziDataArray[imgView.tag];
            NSInteger zanNumber=[dic[@"like_num"] integerValue];
            zanNumber+=1;
            [dic setValue:@"1" forKey:@"is_like"];
            [dic setValue:[NSString stringWithFormat:@"%ld",zanNumber] forKey:@"like_num"];
            TieBaModel *model2=[[TieBaModel alloc]initWithDictionary:dic];
            model2.current_page=model1.current_page;
            model2.from=model1.from;
            model2.to=model1.to;
            model2.last_page=model1.last_page;
            model2.next_page_url=model1.next_page_url;
            model2.per_page=model1.per_page;
            model2.prev_page_url=model1.prev_page_url;
            model2.total=model1.total;
            
            [_myDataArray removeObjectAtIndex:imgView.tag];
            [_myDataArray insertObject:model2 atIndex:imgView.tag];
            
            [_tieziDataArray removeObjectAtIndex:imgView.tag];
            [_tieziDataArray insertObject:dic atIndex:imgView.tag];
            
            //一个cell刷新
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:imgView.tag inSection:0];
            [self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
            //通知后台
            TieBaModel *model3=_myDataArray[imgView.tag];
            [self getZanHttpData:model3.unKnowId];
            
        }else{
            //取消赞
            imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_no"].CGImage;
            NSLog(@"取消赞");
            
            NSDictionary *dic=_tieziDataArray[imgView.tag];
            [dic setValue:@"0" forKey:@"is_like"];
            NSInteger zanNumber=[dic[@"like_num"] integerValue];
            zanNumber-=1;
            [dic setValue:[NSString stringWithFormat:@"%ld",zanNumber] forKey:@"like_num"];
            TieBaModel *model2=[[TieBaModel alloc]initWithDictionary:dic];
            model2.current_page=model1.current_page;
            model2.from=model1.from;
            model2.to=model1.to;
            model2.last_page=model1.last_page;
            model2.next_page_url=model1.next_page_url;
            model2.per_page=model1.per_page;
            model2.prev_page_url=model1.prev_page_url;
            model2.total=model1.total;
            
            [_myDataArray removeObjectAtIndex:imgView.tag];
            [_myDataArray insertObject:model2 atIndex:imgView.tag];
            
            [_tieziDataArray removeObjectAtIndex:imgView.tag];
            [_tieziDataArray insertObject:dic atIndex:imgView.tag];
            
            //一个cell刷新
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:imgView.tag inSection:0];
            [self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
            //通知后台
            TieBaModel *model3=_myDataArray[imgView.tag];
            [self getCancelZanHttpData:model3.unKnowId];
        }
        [imgView.layer addAnimation:k forKey:@"SHOW"];
    }
}


//返回indexPath.row
-(NSInteger)returnIndexPath:(NSString *)topic_id
{
    for (int i=0; i<_myDataArray.count; i++) {
        TieBaModel *model=_myDataArray[i];
        NSInteger modelId=[model.unKnowId integerValue];
        NSInteger myId=[topic_id integerValue];
        if (modelId==myId) {
            return i;
        }
    }
    return 0;
}




-(void)reloadTableView
{
    [self getHttpData];
}



#pragma mark 初始化数据
-(void)initData
{
    
}
//headerView中点击了哪一个
-(void)btnClick:(UIButton *)sender
{
    //NSLog(@"==============================%ld",sender.tag);
    [self.imgDelegate chooseFunction:sender.tag];
}
//tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    cell.backgroundColor=[UIColor whiteColor];
    //cell.userInteractionEnabled=NO;
    
    TieBaModel *model=_myDataArray[indexPath.row];
    
    
    
    //用户头像
    UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 56, 56)];
    [headImageView sd_setImageWithURL:[NSURL URLWithString:model.user_info[@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1"]];
    headImageView.layer.cornerRadius=56/2.0;
    headImageView.layer.masksToBounds=YES;
    [cell.contentView addSubview:headImageView];
    
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:model.user_info[@"avatar_url"]]];
    UIImage *myHeadImage=[UIImage imageWithData:data];
    headImageView.image=myHeadImage;
    
    
    
    if (indexPath.row==0) {
        headImageView.frame=CGRectMake(headImageView.frame.origin.x, headImageView.frame.origin.y-5, headImageView.frame.size.width, headImageView.frame.size.height);
    }
    
    
    
    //盖按钮
    UIButton *headImageBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
    headImageBtn1.frame=headImageView.frame;
    headImageBtn1.tag=indexPath.row+100000;
    [headImageBtn1 addTarget:self action:@selector(headImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:headImageBtn1];
    
    
    CGFloat height=(56-8)/3.0;
    //用户名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, headImageView.frame.origin.y, 80, height)];
    name.text=model.user_info[@"nikename"];
    name.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    name.font=[UIFont systemFontOfSize:13];
    name.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, headImageView.frame.origin.y, [self calculateWidthWithLable:name], height);
    [cell.contentView addSubview:name];
    
    //性别
    UIImageView *sexImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(name.frame)+2, headImageView.frame.origin.y, height, height)];
    [cell.contentView addSubview:sexImageView];
    NSInteger sexState=[model.user_info[@"sex"] integerValue];
    if (sexState==0) {
        sexImageView.image=[UIImage imageNamed:@"sex_man"];
    }else{
        sexImageView.image=[UIImage imageNamed:@"sex_woman"];
    }
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame)+4, 40, height)];
    biaoqian.text=model.tag_name;
    biaoqian.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    biaoqian.font=[UIFont systemFontOfSize:12];
    biaoqian.layer.borderColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1].CGColor;
    biaoqian.layer.borderWidth=1.0;
    biaoqian.layer.cornerRadius=2.0;
    biaoqian.textAlignment=NSTextAlignmentCenter;
    biaoqian.frame=CGRectMake(biaoqian.frame.origin.x, biaoqian.frame.origin.y, [self calculateWidthWithLable:biaoqian]+8, biaoqian.frame.size.height);
    [cell.contentView addSubview:biaoqian];
    
    //时间
    UILabel *time=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(biaoqian.frame)+4, 60, height)];
    time.text=model.created_at;
    time.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    time.font=[UIFont systemFontOfSize:13];
    time.frame=CGRectMake(time.frame.origin.x, time.frame.origin.y, [self calculateWidthWithLable:time], time.frame.size.height);
    [cell.contentView addSubview:time];
    
    
    //CGFloat height2=56/3.0;
    
    //评论
    UIImageView *commentImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-10-30-20-30-20-1, headImageView.frame.origin.y, 20, 20)];
    commentImageView.image=[UIImage imageNamed:@"shequ_comment.png"];
    [cell.contentView addSubview:commentImageView];
    
    
    //评论数量
    UILabel *commentLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(commentImageView.frame)+1, headImageView.frame.origin.y, 30, 20)];
    //commentLab.text=[self getLabel:_dataArray[indexPath.row][@"comments"]];
    commentLab.text=model.comment_num;
    
    commentLab.font=[UIFont systemFontOfSize:10];
    commentLab.textColor=[UIColor grayColor];
    [cell.contentView addSubview:commentLab];
    
    
    
    
    //点赞
    UIImageView *zanImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(commentLab.frame), headImageView.frame.origin.y, 20, 20)];
    //NSInteger zanState=[_dataArray[indexPath.row][@"zanState"] integerValue];
    NSInteger zanState=[model.is_like integerValue];
    if (zanState==0) {
        zanImageView.image=[UIImage imageNamed:@"shequ_dianzan_no.png"];
    }else{
        zanImageView.image=[UIImage imageNamed:@"shequ_dianzan_yes.png"];
    }
    //添加手势
    zanImageView.userInteractionEnabled=YES;
    zanImageView.tag=indexPath.row+1;
    
    
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHappen1:)];
    [zanImageView addGestureRecognizer:tap1];
    
    
    [cell.contentView addSubview:zanImageView];
    
    if (tiebaAnimationState==1) {
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        [zanImageView.layer addAnimation:k forKey:@"SHOW"];
    }
    
    
    
    
    
    
    
    
    
    
    
    
    //    //盖按钮
    //    UIButton *zanImageBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    //    zanImageBtn.frame=zanImageView.frame;
    //    zanImageBtn.tag=indexPath.row+100;
    //    [zanImageBtn addTarget:self action:@selector(zanImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [cell.contentView addSubview:zanImageBtn];
    
    UILabel *zanLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(zanImageView.frame)+1, headImageView.frame.origin.y, 30, 20)];
    //zanLab.text=[self getLabel:_dataArray[indexPath.row][@"zan"]];
    zanLab.text=[self getLabel:model.like_num];
    zanLab.font=[UIFont systemFontOfSize:10];
    zanLab.textColor=[UIColor grayColor];
    [cell.contentView addSubview:zanLab];
    
    
    //精华帖标识
    UIImageView *jinghuatie=[[UIImageView alloc]initWithFrame:CGRectMake(commentImageView.frame.origin.x, CGRectGetMaxY(commentImageView.frame)+3, 20+30+2+20+30, 34*(102/168.0))];
    jinghuatie.image=[UIImage imageNamed:@"tieziImage_01"];
    if (![model.is_popular isEqualToString:@"0"]) {
        jinghuatie.image=[UIImage imageNamed:@"tieziImage_02"];
    }
    if (![model.is_great isEqualToString:@"0"]) {
        jinghuatie.image=[UIImage imageNamed:@"tieziImage_03"];
    }
    
    [cell.contentView addSubview:jinghuatie];
    
    
    //帖子标题
    UILabel *topicLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headImageView.frame)+5, self.frame.size.width-10-10, 100-10-50)];
    //topicLab.text=_dataArray[indexPath.row][@"biaoti"];
    topicLab.text=model.title;
    topicLab.textColor=[UIColor blackColor];
    topicLab.font=[UIFont systemFontOfSize:15];
    topicLab.numberOfLines=0;
    CGSize topicLabSize=[topicLab boundingRectWithSize:CGSizeMake(topicLab.frame.size.width, 0)];
    topicLab.frame=CGRectMake(10, CGRectGetMaxY(headImageView.frame)+5, self.frame.size.width-10-10, topicLabSize.height);
    
    [cell.contentView addSubview:topicLab];
    
    
    //帖子内容
    UILabel *contentLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(topicLab.frame)+5, self.frame.size.width-10-10, 112-10-50-40-1)];
    //contentLab.text=_dataArray[indexPath.row][@"content"];
    contentLab.text=model.content;
    contentLab.textColor=[UIColor blackColor];
    contentLab.font=[UIFont systemFontOfSize:13];
    contentLab.numberOfLines=0;
    CGSize contentLabsize=[contentLab boundingRectWithSize:CGSizeMake(contentLab.frame.size.width, 0)];
    contentLab.frame=CGRectMake(10, CGRectGetMaxY(topicLab.frame)+5, self.frame.size.width-10-10, contentLabsize.height);
    
    [cell.contentView addSubview:contentLab];
    
    //发表的图片
    CGFloat img_w=(self.frame.size.width-10*6)/5.0;
    CGFloat img_h=img_w*1.1;
    NSInteger hang=[self calculatingHang:model.media_urls];
    //NSLog(@"图片有多少行=====%ld",hang);
    for (int i=0; i<hang; i++) {
        for (int j=0; j<5; j++) {
            if (i*5+j<[model.media_urls count]) {
                UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10+(img_w+10)*j, CGRectGetMaxY(contentLab.frame)+10+(img_h+5)*i, img_w, img_h)];
                //imgView.image=[UIImage imageNamed:_dataArray[indexPath.row][@"imageArr"][i*5+j]];
                [imgView sd_setImageWithURL:[NSURL URLWithString:model.media_urls[i*5+j]] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
                //NSLog(@"图片的地址==========%@",model.media_urls[i*5+j]);
                [cell.contentView addSubview:imgView];
                imgView.tag=1000*indexPath.row+i*5+j;
                
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTapHappen:)];
                imgView.userInteractionEnabled=YES;
                [imgView addGestureRecognizer:tap];
            }
        }
    }
    if ([model.media_urls count]>0) {
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h+9, self.frame.size.width, 1)];
        line.backgroundColor=[UIColor colorWithRed:212/255.0 green:213/255.0 blue:214/255.0 alpha:1];
        [cell.contentView addSubview:line];
        if (hang==1) {
            line.frame=CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h+9, self.frame.size.width, 1);
        }else if (hang==2){
            line.frame=CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h*2+5+9, self.frame.size.width, 1);
        }
    }else{
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(contentLab.frame)+9, self.frame.size.width, 1)];
        line.backgroundColor=[UIColor colorWithRed:212/255.0 green:213/255.0 blue:214/255.0 alpha:1];
        [cell.contentView addSubview:line];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)btnClick1:(UIButton *)sender
{
    NSLog(@".........................");
}
//计算评论 点赞的个数
-(NSString *)getLabel:(NSString *)number
{
    NSInteger num=[number floatValue];
    if (num>10000) {
        number=[NSString stringWithFormat:@"%.1f万",num/10000.0];
    }
    return number;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TieBaModel *model=_myDataArray[indexPath.row];
    //帖子标题
    UILabel *topicLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.frame.size.width-10-10, 100-10-50)];
    //topicLab.text=_dataArray[indexPath.row][@"biaoti"];
    topicLab.text=model.title;
    topicLab.textColor=[UIColor blackColor];
    topicLab.font=[UIFont systemFontOfSize:15];
    topicLab.numberOfLines=0;
    CGSize topicLabSize=[topicLab boundingRectWithSize:CGSizeMake(topicLab.frame.size.width, 0)];
    
    //帖子内容
    UILabel *contentLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(topicLab.frame), self.frame.size.width-10-10, 112-10-50-40-1)];
    contentLab.text=model.content;
    contentLab.textColor=[UIColor blackColor];
    contentLab.font=[UIFont systemFontOfSize:13];
    contentLab.numberOfLines=0;
    CGSize contentLabsize=[contentLab boundingRectWithSize:CGSizeMake(contentLab.frame.size.width, 0)];
    //发表的图片
    CGFloat img_w=(self.frame.size.width-10*6)/5.0;
    CGFloat img_h=img_w*1.1;
    if ([model.media_urls count]>0) {
        NSInteger hang=[self calculatingHang:model.media_urls];
        if (hang==1) {
            if (indexPath.row==0) {
                return 60+30+img_h+10+topicLabSize.height+contentLabsize.height-5;
            }else{
                return 60+30+img_h+10+topicLabSize.height+contentLabsize.height;
            }
            
        }else if (hang==2){
            if (indexPath.row==0) {
                return 60+30+img_h*2+5+10+topicLabSize.height+contentLabsize.height-5;
            }else{
                return 60+30+img_h*2+5+10+topicLabSize.height+contentLabsize.height;
            }
            
        }
        
    }
    
    if (indexPath.row==0) {
        return 60+30+topicLabSize.height+contentLabsize.height-5;
    }else{
        return 60+30+topicLabSize.height+contentLabsize.height;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TieBaModel *model=_myDataArray[indexPath.row];
    [self.imgDelegate gotoXiangQingViewController:[model.unKnowId integerValue]];
}
//根据文字计算宽度和高度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.width;
}


//点击发表的图片
-(void)imgTapHappen:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    NSLog(@"手势响应了");
    TieBaModel *model=_myDataArray[imgView.tag/1000];
    //[self.imgDelegate bigImage:imgView.image];
    [self.imgDelegate bigImage:imgView.tag%1000 AndImageArrays:model.media_urls];
}









//头像按钮的响应
-(void)headImageBtnClick:(UIButton *)sender
{
    NSLog(@"=============头像");
    
    TieBaModel *model=_myDataArray[sender.tag-100000];
    
    //进入我关注的界面
    [self.imgDelegate gotoMyAttentionViewController:model.userId];
}

//点赞
-(void)tapHappen1:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    NSLog(@"=========%ld",imgView.tag-1);
    TieBaModel *model1=_myDataArray[imgView.tag-1];
    tiebaAnimationState=1;
    
    NSInteger zanState=[model1.is_like integerValue];
    CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    k.values = @[@(0.1),@(1.0),@(1.5)];
    k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    k.calculationMode = kCAAnimationLinear;
    if (zanState==0) {
        //点赞
        imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_yes"].CGImage;
        
        NSLog(@"点赞");
        
        NSDictionary *dic=_tieziDataArray[imgView.tag-1];
        NSInteger zanNumber=[dic[@"like_num"] integerValue];
        zanNumber+=1;
        [dic setValue:@"1" forKey:@"is_like"];
        [dic setValue:[NSString stringWithFormat:@"%ld",zanNumber] forKey:@"like_num"];
        TieBaModel *model2=[[TieBaModel alloc]initWithDictionary:dic];
        model2.current_page=model1.current_page;
        model2.from=model1.from;
        model2.to=model1.to;
        model2.last_page=model1.last_page;
        model2.next_page_url=model1.next_page_url;
        model2.per_page=model1.per_page;
        model2.prev_page_url=model1.prev_page_url;
        model2.total=model1.total;
        
        [_myDataArray removeObjectAtIndex:imgView.tag-1];
        [_myDataArray insertObject:model2 atIndex:imgView.tag-1];
        
        [_tieziDataArray removeObjectAtIndex:imgView.tag-1];
        [_tieziDataArray insertObject:dic atIndex:imgView.tag-1];
        
        //一个cell刷新
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:imgView.tag-1 inSection:0];
        [self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        
        //通知后台
        TieBaModel *model3=_myDataArray[imgView.tag-1];
        [self getZanHttpData:model3.unKnowId];
        
    }else{
        //取消赞
        imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_no"].CGImage;
        NSLog(@"取消赞");
        
        NSDictionary *dic=_tieziDataArray[imgView.tag-1];
        [dic setValue:@"0" forKey:@"is_like"];
        NSInteger zanNumber=[dic[@"like_num"] integerValue];
        zanNumber-=1;
        [dic setValue:[NSString stringWithFormat:@"%ld",zanNumber] forKey:@"like_num"];
        TieBaModel *model2=[[TieBaModel alloc]initWithDictionary:dic];
        model2.current_page=model1.current_page;
        model2.from=model1.from;
        model2.to=model1.to;
        model2.last_page=model1.last_page;
        model2.next_page_url=model1.next_page_url;
        model2.per_page=model1.per_page;
        model2.prev_page_url=model1.prev_page_url;
        model2.total=model1.total;
        
        [_myDataArray removeObjectAtIndex:imgView.tag-1];
        [_myDataArray insertObject:model2 atIndex:imgView.tag-1];
        
        [_tieziDataArray removeObjectAtIndex:imgView.tag-1];
        [_tieziDataArray insertObject:dic atIndex:imgView.tag-1];
        
        //一个cell刷新
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:imgView.tag-1 inSection:0];
        [self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        
        //通知后台
        TieBaModel *model3=_myDataArray[imgView.tag-1];
        [self getCancelZanHttpData:model3.unKnowId];
    }
    [imgView.layer addAnimation:k forKey:@"SHOW"];
    
}
-(void)zanImageBtnClick:(UIButton *)sender
{
    NSLog(@"点赞");
    
}

//发表的图片
-(void)imgBtnClick:(UIButton *)sender
{
    NSLog(@"图片响应");
    //[self.imgDelegate bigImage:sender.imageView.image];
}
//计算行
-(NSInteger)calculatingHang:(NSArray *)arr
{
    NSInteger hang=0;
    if (arr.count>5) {
        hang=2;
    }else if(arr.count==0){
        hang=0;
    }else{
        hang=1;
    }
    return hang;
}

//获取数据
-(void)getHttpData
{
    tiebaAnimationState=0;
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    // [self isLoading:YES];
    
    [self showCicle:YES];
    
    [manager GET:[NSString stringWithFormat:AllMyCareTopics,_userId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self showCicle:NO];
        [self.header endRefreshing];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (dic) {
            _tieziDataArray=[NSMutableArray arrayWithArray:dic[@"data"]];
            _myDataArray=[NSMutableArray array];

            for (NSDictionary *dic1 in dic[@"data"]) {
                TieBaModel *model=[[TieBaModel alloc]initWithDictionary:dic1];
                model.current_page=dic[@"current_page"];
                model.from=dic[@"from"];
                model.to=dic[@"to"];
                model.last_page=dic[@"last_page"];
                model.next_page_url=dic[@"next_page_url"];
                
                model.per_page=dic[@"per_page"];
                model.prev_page_url=dic[@"prev_page_url"];
                model.total=dic[@"total"];
                [_myDataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self reloadData];
                
            });
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self showCicle:NO];
        [self.header endRefreshing];
        NSLog(@"%@",error.localizedDescription);
    }];
    
}

///帖子点赞
-(void)getZanHttpData:(NSString *)tieziId
{
    NSLog(@"帖子的id======%@",tieziId);
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager POST:[NSString stringWithFormat:@"%@?token=%@&topic_id=%@",TieZiIsLike,token,tieziId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"tieziZanData===============%@点赞结果========%@",dic,dic[@"status"][@"msg"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
//取消点赞
-(void)getCancelZanHttpData:(NSString *)tieziId
{
    NSLog(@"帖子的id======%@",tieziId);
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager DELETE:[NSString stringWithFormat:@"%@?token=%@&topic_id=%@",TieZiCancellike,token,tieziId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"tieziCancelZanData===============%@=====取消赞结果======%@",dic,dic[@"status"][@"msg"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
@end
